package s2.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

import java.io.Serializable;
import java.util.Date;

/**
 * Created by IFT8
 * on 2015/7/13.
 */
public class Person extends ActionSupport implements Serializable {
    private String name;
    private String sex;
    private static int total;
    private String msg;
    private Date birthday;

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }


    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    @Override
    public String toString() {
        return "Person{" +
                "sex='" + sex + '\'' +
                ", name='" + name + '\'' +
                ", total='" + total + '\'' +
                ",brithday='" + birthday + '\'' +
                '}';
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String recMsg() {
        total++;
        System.out.println("Total: " + total);
        System.out.println("birthday: " + birthday);
        setMsg(toString());
        if (total % 2 != 0) {
            return SUCCESS;
        }
        return ERROR;
    }

    public String dynInv() {
        total++;
        ActionContext actionContext = ActionContext.getContext();
        //模拟提交
        actionContext.put("msg", "方法被调用");
        return SUCCESS;
    }

    @Override
    public void validate() {
        if (name==null||"".equals(name)) {
            addFieldError("name","用户名错误");
        }
        super.validate();
    }

    public void validateSex(){
        if(!"Man".equals(sex)||!"Woman".equals(sex)){
            addFieldError("sex","性别错误");
        }
    }
}
